package com.fishing.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import QL.ButlerGameGetRoomConfigRequest;
import QL.ButlerGameGetRoomConfigResponse;
import QL.ButlerGameGetRoomPlatformDetailRequest;
import QL.ButlerGameGetRoomPlatformDetailResponse;
import QL.ButlerGameGetRoomPlatformSummaryRequest;
import QL.ButlerGameGetRoomPlatformSummaryResponse;
import QL.QLClient;

/**
 * 打地鼠数据管控
 * */
@Controller
public class RoomDdsController {

	private static QLClient client = new QLClient();
	
	@RequestMapping("room/dds")
	public ModelAndView dds(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("room/config_dds");
		return mav;
	}
	
	/**
	 * 打地鼠
	 * @param ConfigId 房间ID
	 * */
	@ResponseBody
	@RequestMapping(value="room/dds/table",produces="text/html;charset=UTF8")
	public Map table(String ServerId,String ConfigId,String GameId,String SiteId,HttpServletRequest request) {
		long AllWaterLine = 0;
		long AllFeeGrab = 0;
		long AllBonusPool = 0;
		ButlerGameGetRoomPlatformDetailRequest req = new ButlerGameGetRoomPlatformDetailRequest();
		req.ZoneId = (Integer) request.getSession().getAttribute("ZoneId");
		req.GameId = 4;
		req.SiteId = 0;
		req.ServerId = 0; // 游戏服务器
		req.ConfigId = Integer.parseInt(ConfigId);// 代表房间ID
		req.PlatformId = -1; // 代表所有台子
		ButlerGameGetRoomPlatformDetailResponse rsp = new ButlerGameGetRoomPlatformDetailResponse();
		if (req.ZoneId < 0)
			client.ServerUrl = "http://api2.qq1798.com:8000/router/rest";
		client.Execute(req, rsp);
		// 读取每个房间设置的水位线
		ButlerGameGetRoomConfigRequest req2 = new ButlerGameGetRoomConfigRequest();
		req2.ZoneId = (Integer) request.getSession().getAttribute("ZoneId");
		req2.GameId = 4;
		req2.SiteId = 0;
		ButlerGameGetRoomConfigResponse rsp2 = new ButlerGameGetRoomConfigResponse();
		client.Execute(req2, rsp2);

		if (rsp2.ErrMsg == "" || rsp2.ErrMsg == null) {
			// 得到对应房间的水位设置,来与当前水位比较,来显示当前水位的难度
			String[] difficult = rsp2.ConfigList.get((int) req.ConfigId - 1).DifficultyBoundary.split(",");
			// 得到当前台子总共有多少难度级别
			List<String> difficultAllLevelList = Arrays.asList(difficult);
			List list = new ArrayList();

			for (int i = 0, size = rsp.DetailList.size(); i < size; i++) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("PlatformId", rsp.DetailList.get(i).PlatformId);// 当前台子ID
				map.put("WaterLine", rsp.DetailList.get(i).WaterLine);// 当前台子水位线
				map.put("FeeGrab", rsp.DetailList.get(i).FeeGrab);// 当前台子总税收
				map.put("DifficultAllLevel", difficultAllLevelList.size() + 1);// 当前台子共有多少难度级别
				map.put("WaterLineLimit", difficult);// 当前台子水位线界限

				// 得到当前水位线对应的难度级别
				int j = 0;
				for (j = 0; j < difficultAllLevelList.size(); j++) {
					if ((rsp.DetailList.get(i).WaterLine <= Long.parseLong(difficultAllLevelList.get(j)) && j == 0)
							|| (rsp.DetailList.get(i).WaterLine <= Long.parseLong(difficultAllLevelList.get(j))
									&& j > 0)) {
						map.put("DifficultLevel", (j + 1) + "级");
						break;
					}
				}
				if (j == difficultAllLevelList.size()) {
					map.put("DifficultLevel", (j + 1) + "级");
				}
				list.add(map);
			}
			Map<String, Object> mapdata = new HashMap();
			mapdata.put("status", "200");
			mapdata.put("message", "");
			mapdata.put("count", list.size());
			mapdata.put("data", list);
			return mapdata;
		} else {
			Map<String, Object> mapdata = new HashMap();
			mapdata.put("status", "200");
			mapdata.put("message", "");
			mapdata.put("count", 0);
			mapdata.put("data", "");
			return mapdata;
		}
	}
}
